#############################################################################
# project: Prioritizing Exceptional Social Needs 
# file: 04_analysis.R 
# author: Brian Dietrich
# task: creating conjoint plots
# input: pooled_df2
# last revision: 2023/07/11
# output: conjoint plots 
#############################################################################



# IMPORTANT: Please execute scripts 01 to 03 first ###

#source("01_preparation_function.R")
#source("02_plot_function.R")
#source("03_data_prep_and_merge.R")


### Figure 1. Baseline results from the general population sample

# create data for conjoint plot
marginal_means_grouped_sector <- cj(data = pooled_df2,
                                formula = selected ~
                                  Gender +
                                  Age +
                                  Citizenship +
                                  Composition + 
                                  Unemployment + 
                                  Supportive +
                                  Citizenship +
                                  Since +
                                  Education,
                                estimate = "mm",
                                by = ~ sector,
                                id = ~ respondentIndex)

# create plot (in color)
plot_group_colour(marginal_means_grouped_sector, marginal_means_grouped_sector$sector) + expand_limits(y=c(0.2, 0.8))
ggsave(file = "../figures/figure_01.png", width = 8, height = 8)



### Figure 2. Differences in marginal means from the general population sample. 

marginal_means_grouped_sector_mmdiff <- cj(data = pooled_df2,
                                formula = selected ~ 
                                  Gender + 
                                  Age + 
                                  Citizenship + 
                                  Composition + 
                                  Unemployment + 
                                  Supportive + 
                                  Since +
                                  Education,
                                estimate = "mm_diff",
                                by = ~ sector,
                                id = ~ respondentIndex)

plot_group_colour(marginal_means_grouped_sector_mmdiff,
                  marginal_means_grouped_sector_mmdiff$sector,
                  vline = 0) + scale_y_continuous(limits = c(-0.1, 0.1)) 
ggsave(file = "../figures/figure_02.png", width = 8, height = 8)



### Figure 3. Baseline results from student sample. 

marginal_means_grouped_student <- cj(data = pooled_df2,
                                  formula = selected ~ 
                                    Gender + 
                                    Age + 
                                    Citizenship + 
                                    Composition +
                                    Unemployment +
                                    Supportive + 
                                    Since + 
                                    Education, 
                                  estimate = "mm",
                                  by = ~ student,
                                  id = ~ respondentIndex)

plot_group_colour(marginal_means_grouped_student, marginal_means_grouped_student$student) + expand_limits(y=c(0.2, 0.8)) 
ggsave(file = "../figures/figure_03.png", width = 8, height = 8)



#### Figure 4.Differences in marginal means from the student sample.

marginal_means_grouped_student_mmdiff <- cj(data = pooled_df2,
                                  formula = selected ~ 
                                    Gender + 
                                    Age + 
                                    Citizenship + 
                                    Composition +
                                    Unemployment +
                                    Supportive + 
                                    Since + 
                                    Education, 
                                  estimate = "mm_diff",
                                  by = ~ student,
                                  id = ~ respondentIndex)

plot_group_colour(marginal_means_grouped_student_mmdiff,
                  marginal_means_grouped_student_mmdiff$student,
                  vline = 0) + scale_y_continuous(limits = c(-0.15, 0.15))  
ggsave(file = "../figures/figure_04.png", width = 8, height = 8)



### Figure A2. Baseline results from the general population sample with weights
# for balance between public employees and citizens from the private sector 

x1 <- cj(data = match_cj,
         formula = selected ~ 
           Gender + 
           Age + 
           Citizenship + 
           Composition + 
           Unemployment + 
           Supportive + 
           Since +
           Education,
         estimate = "mm",
         by = ~ sector,
         weights = ~ ebal,
         id = ~ respondentIndex)

plot_group_colour(x1, 
                  x1$sector) + expand_limits(y=c(0.2, 0.8)) 
ggsave(file = "../figures/figure_A2.png", width = 8, height = 8)




### Figure A3. Baseline results from the general population sample for interaction between nationality and household composition 

marginal_means_grouped_sector_children <- cj(data = pooled_df2,
                                    formula = selected ~
                                      Children,
                                    estimate = "mm",
                                    by = ~ sector,
                                    id = ~ respondentIndex)

plot_group_colour(marginal_means_grouped_sector_children, marginal_means_grouped_sector_children$sector) + expand_limits(y=c(0.2, 0.8))
ggsave(file = "../figures/figure_A3.png", width = 8, height = 3)







### Figure A4. Baseline results from the student sample for the interaction between nationality and household composition 

marginal_means_grouped_student_children <- cj(data = pooled_df2,
                                             formula = selected ~
                                               Children,
                                             estimate = "mm",
                                             by = ~ student,
                                             id = ~ respondentIndex)

plot_group_colour(marginal_means_grouped_student_children, marginal_means_grouped_student_children$student) + expand_limits(y=c(0.2, 0.8))
ggsave(file = "../figures/figure_A4.png", width = 8, height = 3)

###### end #####